package com.example.clock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.clock.entity.User;
import com.example.clock.mapper.UserMapper;
import com.example.clock.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServicesImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    @Async
    public void updateUserInfo(User user) {
        User userDb = this.baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, user.getId()));
        if (ObjectUtils.isEmpty(userDb)) {
            user.setCreateTime(LocalDateTime.now());
            user.setLastLoginTime(LocalDateTime.now());
            this.baseMapper.insert(user);
        } else {
            user.setLastLoginTime(LocalDateTime.now());
            this.baseMapper.updateById(user);
        }
    }
}
